﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }
        #region 友好锁
        private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
        private string Tname = "";
        private async void button2_Click(object sender, EventArgs e)
        {
            // 标记Main方法为异步，以便能够使用await关键字
            Console.WriteLine("Starting tasks...");
 
            // 创建并启动三个任务
            Task task1 = Task.Run(async () => await DoWorkAsync("Task 1", 5100));
            Task task2 = Task.Run(async () => await DoWorkAsync("Task 2", 4100));
            Task task3 = Task.Run(async () => await DoWorkAsync("Task 3", 7100));

            // 等待所有任务完成
            await Task.WhenAll(task1, task2, task3);

            Console.WriteLine("All tasks completed.");
        }
        async Task DoWorkAsync(string taskName, int delay)
        {
            // 请求进入临界区
            await _semaphore.WaitAsync();

            try
            {
                // 安全地访问和修改 Tname
                Tname = taskName;
                // 模拟工作负载
                Console.WriteLine($"{taskName} is starting.  {Tname}   starttime:{ DateTime.Now}");
                await Task.Delay(delay); // 异步等待指定的时间（毫秒），不会阻塞线程
                Console.WriteLine($"{taskName} is completed. {Tname}   endtime:{DateTime.Now}");
            }
            finally
            {
                // 释放临界区
                _semaphore.Release();
            }
        }
        #endregion

        #region Lock锁
        static object _lock = new object();
        private string Tname1 = "";
        private async void button1_Click(object sender, EventArgs e)
        {
            // 标记Main方法为异步，以便能够使用await关键字
            Console.WriteLine("Starting tasks...");

            // 创建并启动三个任务
            Task task1 = Task.Run(async () => await DoWorkAsync1("Task 1", 5000));
            Task task2 = Task.Run(async () => await DoWorkAsync1("Task 2", 7000));
            Task task3 = Task.Run(async () => await DoWorkAsync1("Task 3", 9500));

            // 等待所有任务完成
            await Task.WhenAll(task1, task2, task3);

            Console.WriteLine("All tasks completed.");
        }
        async Task DoWorkAsync1(string taskName, int delay)
        {
            lock (_lock)
            {
                // 安全地访问和修改 Tname
                Tname = taskName;
                Console.WriteLine($"{taskName} is starting.  {Tname}   starttime:{DateTime.Now}");
            }
            // 模拟工作负载
            await Task.Delay(delay); // 异步等待指定的时间（毫秒），不会阻塞线程
            lock (_lock)
            {
                // 安全地访问和修改 Tname
                Console.WriteLine($"{taskName} is completed. {Tname}   endtime:{DateTime.Now}");
            }
          

        }
        #endregion
    }
}
